<?php

/**
 * PersonalMemberData filter form base class.
 *
 * @package    Wildlive
 * @subpackage filter
 * @author     Maximilian Lenkeit <dev@lenki.com>
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BasePersonalMemberDataFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'member_id'                  => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Member'), 'add_empty' => true)),
      'bank_account_id'            => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('BankAccount'), 'add_empty' => true)),
      'email_address'              => new sfWidgetFormFilterInput(),
      'date_of_birth'              => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'birthplace'                 => new sfWidgetFormFilterInput(),
      'direct_debit_authorization' => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'created_at'                 => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'updated_at'                 => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'deleted_at'                 => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'created_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'add_empty' => true)),
      'updated_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Updator'), 'add_empty' => true)),
      'deleted_by'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Deletor'), 'add_empty' => true)),
      'version'                    => new sfWidgetFormFilterInput(),
      'addresses_list'             => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Address')),
      'phone_numbers_list'         => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'PhoneNumber')),
    ));

    $this->setValidators(array(
      'member_id'                  => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Member'), 'column' => 'id')),
      'bank_account_id'            => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('BankAccount'), 'column' => 'id')),
      'email_address'              => new sfValidatorPass(array('required' => false)),
      'date_of_birth'              => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false)))),
      'birthplace'                 => new sfValidatorPass(array('required' => false)),
      'direct_debit_authorization' => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'created_at'                 => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'updated_at'                 => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'deleted_at'                 => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'created_by'                 => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Creator'), 'column' => 'id')),
      'updated_by'                 => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Updator'), 'column' => 'id')),
      'deleted_by'                 => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('Deletor'), 'column' => 'id')),
      'version'                    => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'addresses_list'             => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Address', 'required' => false)),
      'phone_numbers_list'         => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'PhoneNumber', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('personal_member_data_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addAddressesListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.MemberAddress MemberAddress')
      ->andWhereIn('MemberAddress.address_id', $values)
    ;
  }

  public function addPhoneNumbersListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.MemberPhoneNumber MemberPhoneNumber')
      ->andWhereIn('MemberPhoneNumber.phone_number_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'PersonalMemberData';
  }

  public function getFields()
  {
    return array(
      'id'                         => 'Number',
      'member_id'                  => 'ForeignKey',
      'bank_account_id'            => 'ForeignKey',
      'email_address'              => 'Text',
      'date_of_birth'              => 'Date',
      'birthplace'                 => 'Text',
      'direct_debit_authorization' => 'Boolean',
      'created_at'                 => 'Date',
      'updated_at'                 => 'Date',
      'deleted_at'                 => 'Date',
      'created_by'                 => 'ForeignKey',
      'updated_by'                 => 'ForeignKey',
      'deleted_by'                 => 'ForeignKey',
      'version'                    => 'Number',
      'addresses_list'             => 'ManyKey',
      'phone_numbers_list'         => 'ManyKey',
    );
  }
}
